#include "BulletsBank.h"
#include "GameConfig.h"
#include "Config.h"
#include "pugixml.hpp"
#include "BulletFactory.h"

using namespace pugi;

BulletsBank::BulletsBank(void)
{
	_listBulletFactory = NULL;
	_isInit = false;
}


BulletsBank::~BulletsBank(void)
{
	CC_SAFE_RELEASE(_listBulletFactory);
}

BulletsBank* BulletsBank::sharedBulletBank()
{
	if (!_instance)
		_instance = new BulletsBank();
	return _instance;
}

void BulletsBank::initBank()
{
	setBulletFactories(CCDictionary::create());

	string data = string(dynamic_cast<CCString*>(GameConfig::sharedGameConfig()->getConfigData()->objectForKey(CONFIG_BULLETS))->getCString());

	xml_document doc;
	xml_parse_result result = doc.load(data.c_str());
	//CCAssert(result.status == status_ok, "FAILD");

	xml_node node = doc.child("Root").child("Bullets");

	//char* temp = new char(20);
	for (xml_node i = node.child("Bullet"); i; i = i.next_sibling("Bullet"))
	{
		BulletFactory* bFactory = BulletFactory::create();
		bFactory->parseXml(i);
		_listBulletFactory->setObject(bFactory, bFactory->getId()->getCString());
	}
}

void BulletsBank::clearBank()
{
	delete _instance;
	_instance = NULL;
}

BulletsBank* BulletsBank::_instance = NULL;
